package com.hzlx;

import com.hzlx.dto.ProductRequest;
import com.hzlx.dto.ProductResponse;
import com.hzlx.dto.ResultDto;
import jakarta.annotation.Resource;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

@RestController
public class ProductController {
    @Resource RestTemplate restTemplate;
    @PostMapping("/api/product")
    @ResponseStatus(HttpStatus.CREATED)
    public void create(@RequestBody ProductRequest productRequest){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity(productRequest, headers);
        restTemplate.postForLocation("http://product-service/api/product", entity);
    }
    @GetMapping("/api/product")
    @ResponseStatus(HttpStatus.OK)
    public ResultDto<List<ProductResponse>> findAll(){
        ParameterizedTypeReference<ResultDto<List<ProductResponse>>> pt = new ParameterizedTypeReference<>() {};
        ResponseEntity<ResultDto<List<ProductResponse>>> exchange
                = restTemplate.exchange("http://product-service/api/product", HttpMethod.GET, HttpEntity.EMPTY, pt);
        return exchange.getBody();
    }
}
